{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4_Python流程控制\n",
    "\n",
    "条件语句/循环语句/循环控制语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、 Python 条件语句\n",
    "Python条件语句是通过一条或多条语句的执行结果（True或者False）来决定执行的代码块。\n",
    "\n",
    "Python程序语言指定任何非0和非空（null）值为true，0 或者 null为false。\n",
    "\n",
    "if 语句的判断条件可以用>（大于）、<(小于)、==（等于）、>=（大于等于）、<=（小于等于）来表示其关系。 \n",
    "\n",
    "Python 编程中 if 语句用于控制程序的执行，基本形式为:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "if 判断条件：\n",
    "\n",
    "    执行语句……\n",
    "    \n",
    "else：\n",
    "\n",
    "    执行语句……"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Stocks has APPLE\n"
     ]
    }
   ],
   "source": [
    "Stocks = ['APPLE', 'BABA']\n",
    "\n",
    "if 'APPLE' in Stocks:\n",
    "    print('Stocks has APPLE')\n",
    "else:\n",
    "    print('Stocks has not APPLE')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Stocks has not TENCENT and SBUX\n"
     ]
    }
   ],
   "source": [
    "if 'TENCENT' in Stocks:\n",
    "    print('Stocks has TENCENT')\n",
    "elif 'SBUX' in Stocks:\n",
    "    print('Stocks has SBUX')\n",
    "else:\n",
    "    print('Stocks has not TENCENT and SBUX')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、 Python循环语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### while\n",
    "\n",
    "Python 编程中 while 语句用于循环执行程序，即在某条件下，循环执行某段程序，以处理需要重复处理的相同任务。其基本形式为：\n",
    "\n",
    "while 判断条件：\n",
    "\n",
    "    执行语句……"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "over\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "\n",
    "while count<6:\n",
    "    print(count)\n",
    "    count+=1\n",
    "print('over')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### for\n",
    "\n",
    "Python for循环可以遍历任何序列的项目，如一个列表或者一个字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "over\n"
     ]
    }
   ],
   "source": [
    "l = [0,1,2,3,4,5]\n",
    "\n",
    "for i in l:\n",
    "    print(i)\n",
    "print('over')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "over\n"
     ]
    }
   ],
   "source": [
    "for i in range(6):\n",
    "    print(i)\n",
    "    \n",
    "print('over')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C\n",
      "h\n",
      "a\n",
      "n\n",
      "n\n",
      "e\n",
      "l\n",
      "over\n"
     ]
    }
   ],
   "source": [
    "for s in 'Channel':\n",
    "    print(s)\n",
    "print('over')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a : 1\n",
      "b : 2\n"
     ]
    }
   ],
   "source": [
    "d = {'a':1, 'b':2}\n",
    "\n",
    "for k in d:\n",
    "    print(k,':', d[k])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**麦肯锡面试题目**\n",
    "\n",
    "题目：如何用一行代码将输入[True,False,False,True,True]，输出为[0,3,4]。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "TFL = [True,False,False,True,True]\n",
    "\n",
    "l = []\n",
    "\n",
    "for tf in range(len(TFL)):\n",
    "    if TFL[tf]==True:\n",
    "        l.append(tf)\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 3, 4]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[tf for tf in range(len(TFL)) if TFL[tf]==True]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Python 循环嵌套**\n",
    "\n",
    "Python 语言允许在一个循环体里面嵌入另一个循环。\n",
    "\n",
    "题目： 输出[1,3,5]不重复的所有数的组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "l = [1,3,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 3 5\n",
      "1 5 3\n",
      "3 1 5\n",
      "3 5 1\n",
      "5 1 3\n",
      "5 3 1\n"
     ]
    }
   ],
   "source": [
    "for i in l:\n",
    "    for j in l:\n",
    "        for k in l:\n",
    "            if (i!=j) and (i!=k) and (j!=k):\n",
    "                print(i,j,k)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、 循环控制语句(break、 continue、 pass)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**终止break**\n",
    "\n",
    "break语句用来终止循环语句，即循环条件没有False条件或者序列还没被完全递归完，也会停止执行循环语句。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Q\n",
      "a\n",
      "u\n",
      "n\n",
      "t\n"
     ]
    }
   ],
   "source": [
    "s = 'QauntitativeTrader'\n",
    "\n",
    "for i in s:\n",
    "    if i =='i':\n",
    "        break\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**跳过continue**\n",
    "\n",
    "continue 语句用来告诉Python跳过当前循环的剩余语句，然后继续进行下一轮循环。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Q\n",
      "a\n",
      "u\n",
      "n\n",
      "t\n",
      "t\n",
      "a\n",
      "t\n",
      "v\n",
      "e\n",
      "T\n",
      "r\n",
      "a\n",
      "d\n",
      "e\n",
      "r\n"
     ]
    }
   ],
   "source": [
    "for i in s:\n",
    "    if i =='i':\n",
    "        continue\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**pass 标明但不跳过**\n",
    "\n",
    "Python pass是空语句，是为了保持程序结构的完整性。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Q\n",
      "a\n",
      "u\n",
      "n\n",
      "t\n",
      "run pass\n",
      "i\n",
      "t\n",
      "a\n",
      "t\n",
      "run pass\n",
      "i\n",
      "v\n",
      "e\n",
      "T\n",
      "r\n",
      "a\n",
      "d\n",
      "e\n",
      "r\n"
     ]
    }
   ],
   "source": [
    "for i in s:\n",
    "    if i =='i':\n",
    "        pass\n",
    "        print('run pass')\n",
    "    print(i)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
